<?php

/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/5
 * Time: 13:28
 */
namespace app\api\controller;

use app\adminapiv3\service\ServerAuth;
use app\common\service\Base as AppBase;

class Base extends AppBase
{
    function __construct()
    {
        global $errCode;
        $errCode = $this->CODE_SUCCESS;
        global $errMsg;
        $errMsg = 'SUCCESS';
    }

    /**
     * @name 检查是否返回正确
     * @param $server
     * @author wx
     * @date 2019/11/7 15:38
     */
    function checkErrr($server)
    {
        if ($server->CODE != $server->CODE_SUCCESS) {
            returnJson($server->CODE, $server->MSG);
        }
    }

    /**
     * @name: 获取数据
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-06 17:08:41
     */
    function filterInput ()
    {
        $inputData = file_get_contents('php://input', 'r');


        if (empty($inputData))
        {
            $this->jsonErrOut("没有接受到参数");
        }


        $jsonData = base64_decode($inputData);

        //模拟注册信息
//         $jsonData = '{
//                        "version":"3.0.1.1",
//                        "is_software":true,
//                        "alias":"DESKTOP-K7GCESM",
//                        "os":"windows",
//                        "osversion":"Windows 2016 64Bit",
//                        "networkname":"eth0",
//                        "hardware":{"Cpu":11,"Inflow":0,"Memory":45,"Netflow":0,"Outflow":0},
//                        "ipaddr":"3.0.0.0",
//                        "bind_key":"4cd75003005d3aef10a6c4694fa37a0c",
//                        "host_info":{"Mac":"3.0.0.0","Cpu":"3.0.0.0"}
//                    }';


        $postData = json_decode($jsonData, true);


        if (empty($postData))
        {
            $this->jsonErrOut("JSON 错误 !!");
        }

        $controller = strtolower(request()->controller());
        $action =  strtolower(request()->action());


        if($controller == 'register' && $action == 'reg'){
            //登录
            if(!isset($postData['host_info']) || !isset($postData['bind_key'])){
                $this->jsonErrOut("数据错误 缺少必要参数");
            }
        }else{
            //非登录
            if (!isset($postData['hostkey'])) {
                $this->jsonErrOut("数据错误 缺少必要参数hostkey");
            }

        }

        if (!isset($postData['version'])) {
            $this->jsonErrOut("数据错误 版本号必须");
        }

        global $online_host;
        $online_host = [];
        if (isset($postData['host_id']))
        {
            $online_host[$postData['host_id']] = [
                'id' => $postData['host_id'],
            ];
        }

        return $postData;
    }

    /**
     * @name: json输出
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-05-22 16:31:42
     */
    public function jsonOutPut ($data = [])
    {
        global $errCode;
        global $errMsg;

        $authServer = new ServerAuth();
        $version = $authServer->getPlatformVersion();

        $outData = [
            'code' => $errCode,
            'message' => $errMsg,
            'version' => $version,
        ];

        isset($data['data']) ? $outData = array_merge($outData, $data) : $outData['data'] = $data;
        echo json_encode($outData, JSON_UNESCAPED_UNICODE);die;
    }

    /**
     * @name 输出错误信息
     * @author wx
     * @date 2020/7/27 14:21
     */
    public function jsonErrOut( $msg = 'error', $code = 202, $data = array())
    {
        $versionPath = './config/serverVersion.conf';
        if (file_exists($versionPath)) {
            $versionPath = file_get_contents($versionPath);
        }

        $outData = [
            'code' => $code,
            'message' => $msg,
            'version' => $versionPath,
        ];

        isset($data['data']) ? $outData = array_merge($outData, $data) : $outData['data'] = $data;

        echo json_encode($outData, JSON_UNESCAPED_UNICODE);die;
    }
}